package lol.clann.minecraft.springboot.api.model;

import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 *
 * @author pyz
 * @date 2019/6/5 11:54 PM
 */
public class Slot implements Iterator<Slot> {
    private final int index;
    private final Inventory inventory;

    public Slot(int index, Inventory inventory) {
        this.index = index;
        this.inventory = inventory;
    }

    public ItemStack getItem() {
        return inventory.getItem(index);
    }

    public Slot setItem(ItemStack itemStack) {
        inventory.setItem(index, itemStack);
        return this;
    }

    public int getIndex() {
        return index;
    }

    @Override
    public boolean hasNext() {
        return index < inventory.getSize() - 1;
    }

    @Override
    public Slot next() {
        if (hasNext()) {
            return new Slot(index + 1, inventory);
        }
        throw new NoSuchElementException();
    }

    public boolean hasPrevious() {
        return index > 0;
    }

    public Slot previous() {
        if (hasPrevious()) {
            return new Slot(index - 1, inventory);
        }
        throw new NoSuchElementException();
    }

    public int nextIndex() {
        return index + 1;
    }

    public int previousIndex() {
        return index - 1;
    }

}
